#include "stdafx.h"
#include "Serializable.h"
#include <QtCore/QMetaObject>
#include <QtCore/QMetaProperty>
#include "moc_Serializable.cpp"

#pragma region Constructor

Serializable::Serializable(void)
{
}


Serializable::~Serializable(void)
{
}

#pragma endregion

#pragma region Public Methods

#pragma endregion

#pragma region Helper Methods

bool Serializable::SerializeProps(QDomDocument* doc, QDomElement *parent)
{
	const QMetaObject *metaobject = metaObject();
	int count = metaobject->propertyCount();
	for (int i=0; i<count; ++i) 
	{
		QMetaProperty metaproperty = metaobject->property(i);
		const char *name = metaproperty.name();

		if (metaproperty.isStored(this))
		{
			QVariant value = property(name);

			QDomElement tag = doc->createElement(name);
			parent->appendChild(tag);

			QDomText text = doc->createTextNode(value.toString());
			tag.appendChild(text);
		}
	}

	return true;
}

bool Serializable::DeserializeProps(QDomDocument* doc, QDomElement *parent)
{
	const QMetaObject *metaobject = metaObject();
	int count = metaobject->propertyCount();
	for (int i=0; i<count; ++i) 
	{
		QMetaProperty metaproperty = metaobject->property(i);
		const char *name = metaproperty.name();

		if (metaproperty.isStored(this))
		{
			QDomElement tag = parent->firstChildElement(name);
			if (!tag.isNull())
			{
				setProperty(name, QVariant(tag.text()));
			}
		}
	}

	return true;
}

#pragma endregion
